import {defineStore} from "pinia"
import {bs} from "@/common/utils"
import {storePrefix} from "../config"

import zhCN from 'ant-design-vue/es/locale/zh_CN';
import enUS from 'ant-design-vue/es/locale/en_US';

import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';

export type langType='zh' | 'en'

export function getCurrentLang():langType{
  try {
    const dataStr=bs.getItem(`${storePrefix}_${langStoreId}`)
    const data=JSON.parse(dataStr)
    if(data&&data.lang) return data.lang
  } catch (error) {
    return initLang
  }
  return initLang
}

type LangMapType=Record<langType,any>

const antdvueLangMap:LangMapType={
  zh:zhCN,
  en:enUS,
}

const dayjsLangMap:LangMapType={
  zh:'zh-cn',
  en:'en',
}

export const langStoreId='lang'

export const initLang:langType="zh"

dayjs.locale(dayjsLangMap[getCurrentLang()])

export default defineStore(langStoreId,{
    state:()=>({
      lang:initLang as langType,
      antdLang:antdvueLangMap[initLang],
    }),
    actions:{
      setLang(lang:langType){
        this.antdLang=antdvueLangMap[lang]
        dayjs.locale(dayjsLangMap[lang])
        this.lang=lang
      }

    },
    

})